package test.client;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import org.je.ufo.Sleep;
import com.fyf.game.net.business.Cmd;
import com.fyf.game.net.client.MessageChannel;

public class ChartUI extends JFrame implements WindowListener
{
	public String				ip;
	public int					port;

	private static final long	serialVersionUID	= 1L;
	private MessageChannel		channel;

	public static void main(String... args) {
		UIShared.init();
		UIShared.chartUI.doShow();
		UIShared.showArea.requestFocus();
	}

	public ChartUI(int w, int h) {
		super("聊天界面");
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.setLayout(null);
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		this.setBounds( (dim.width - w) / 2 , (dim.height - h) / 2 ,w ,h);
		this.add(UIShared.showJs);
		this.add(UIShared.userJs);
		this.add(UIShared.js);
		this.addWindowListener(this);
		this.setResizable(false);
	}

	public void doShow() {
		this.setVisible(true);
	}

	public boolean isConnected() {
		return channel != null && channel.isClosed() == false;
	}

	private boolean	startUpdater	= false;

	public void connect() {
		try {
			if (channel != null) {
				channel.close();
			}
			channel = new MessageChannel(new ChartReceiveHandler());
			channel.connect(ip ,port);

			if (startUpdater) {
				return;
			}
			startUpdater = true;
			new UserUpdater().start();
		} catch (Exception ex) {}
	}

	public void login(String id, String name, String password) {
		try {
			channel.send(Cmd.login + Cmd.spr + id + Cmd.spr + name + Cmd.spr + password);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void logout() {
		try {
			if (channel != null && channel.isClosed() == false)
				channel.send(Cmd.logout);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void disconnect() {
		try {
			if (channel != null && channel.isClosed() == false)
				channel.send("");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void scanUsers() {
		try {
			if (channel != null && channel.isClosed() == false)
				channel.send(Cmd.scan_user);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void chart(String msg) {
		try {
			if (channel != null && channel.isClosed() == false)
				channel.send(Cmd.chart + Cmd.spr + Cmd.chart_to_all + Cmd.spr + msg);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void windowClosing(WindowEvent e) {
		try {
			logout();
		} catch (Exception ex) {}
		try {
			disconnect();
		} catch (Exception ex) {}

		while( true ) {
			if (channel == null || channel.isClosed()) {
				break;
			}
			Sleep.millis(20);
		}
		dispose();
	}

	public void windowClosed(WindowEvent e) {
		System.exit(0);
	}

	public void windowOpened(WindowEvent e) {}

	public void windowIconified(WindowEvent e) {}

	public void windowDeiconified(WindowEvent e) {}

	public void windowActivated(WindowEvent e) {}

	public void windowDeactivated(WindowEvent e) {}
}
